<?php
/**
 * Enter description here...
 *
 */
class UserForm extends Zend_Form
{
	var $cities = array(
		'Ha Noi',
		'Ho Chi Minh',
		'Da Nang',
		'An Giang',
		'Bac Can',
		'Bac Giang',
		'Bac Ninh',
		'Ben Tre',
		'Bing Duong',
		'Binh Oinh',
		'Binh Phuoc',
		'Binh Thuan',
		'Can Tho',
		'Cao Bang',
		'Dac lac',
		'Dong Nai',
		'Dong Thap',
		'Gia Lai',
		'Ha Giang',
		'Ha Nam',
		'Ha Tay',
		'Ha Tinh',
		'Hai Duong',
		'Hung Yen',
		'Hai Phong',
		'Hoa Binh',
		'Khanh Hoa',
		'Kien Giang',
		'Kon Tum',
		'Lai Chau',
		'Lam Dong',
		'Lang Son',
		'Lao Cai',
		'Long An',
		'Minh Hai',
		'Nam Dinh',
		'Nam Ha',
		'Nghe An',
		'Ninh Binh',
		'Ninh Thuan',
		'Phu Yen',
		'Phu Tho',
		'Quang Binh',
		'Quang Nam',
		'Quang Ngai',
		'Quang Ninh',
		'Quang Tri',
		'Soc Trang',
		'Son La',
		'Song Be',
		'Tay Ninh',
		'Thai Binh',
		'Thai Nguyen',
		'Thanh Hoa',
		'Thua Thien Hue',
		'Tien Giang',
		'Tra Vinh',
		'Tuyen Quang',
		'Vinh Long',
		'Vinh Phuc',

		'Vung Tau',
		'Yen Bai'
	);
	/**
	 * Enter description here...
	 *
	 * @param unknown_type $userType
	 * @param unknown_type $options
	 */
	public function __construct($userType, $options = null)
	{
		parent::__construct($options);
		$this->addPrefixPath('Vmg_Form_Decorator', 'Vmg/Form/Decorator', 'decorator');

		// load translator
		$registry = Zend_Registry::getInstance();
		$translate = $registry->get('Zend_Translate');
		$access_levels = $registry->get('access_level_config');
		$others_config = $registry->get('others_config');

		// set up form
		$this->setName('user');
		$this->setAttrib('id', 'userForm');

		// set up empty validator
		$empty_validator = new Zend_Validate_NotEmpty();
		$empty_validator->setMessage("This field is not left empty");
		$empty_validator->setTranslator($translate);

		// set up email validator
		$email_validator = new Zend_Validate_EmailAddress();
		$email_validator->setMessage("This is not an valid email address");
		$email_validator->setTranslator($translate);

		// set up number validator
		$number_validator = new Zend_Validate_Digits();
		$number_validator->setMessage("This field is only allowed numbers to be keyed in");
		$number_validator->setTranslator($translate);

		// set up name validator
		$uname_validator = new Zend_Validate_Regex('/^[a-zA-Z0-9_]+$/');
		$uname_validator->setMessage("The username must be built from this set of characters a-zA-Z0-9_");
		$uname_validator->setTranslator($translate);

		// set string length validator
		$phone_validator = new Zend_Validate_StringLength();
		$phone_validator->setMessage("The length of this field must be between %min% and %max%");

		// set up form elements
		$id = new Zend_Form_Element_Hidden('id'); //id

		$user_name = new Zend_Form_Element_Text('nameUserForm'); // user name
		$user_name->setLabel($translate->_("Name") )
		->setRequired(true)
		->addFilter('StripTags')
		->addFilter('StringTrim')
		->setAttrib('size', 15)
		->setDecorators(array('Composite'))
		->addValidator($empty_validator);

		$user_type = new Zend_Form_Element_Select('typeUserForm'); // type
		$user_type->setLabel($translate->_("Type of users") )
		->setRequired(true)
		->addFilter('StripTags')
		->addFilter('StringTrim')
		->setDecorators(array('Composite'))
		;

		$types = new UserTypes();
		$type_array =  $types->fetchAll('access_level = ' . $userType);
		foreach($type_array as $type){
			$user_type->addMultiOption((int)$type->id, (string)$type->type_name);
		}

		$user_email = new Zend_Form_Element_Text('emailUserForm'); // email
		$user_email->setLabel($translate->_("Email") )
		->setRequired(true)
		->addFilter('StripTags')
		->addFilter('StringTrim')
		->addValidator($email_validator)
		->setDecorators(array('Composite'))
		->setAttrib('size', 30);
		;

		$user_city = new Zend_Form_Element_Select('cityUserForm'); // address
		$user_city->setLabel($translate->_("City") )
		->addFilter('StripTags')
		->addFilter('StringTrim')
		->setDecorators(array('Composite'))
		->addValidator($empty_validator);

		foreach($this->cities as $city){
			$user_city->addMultiOption($city, $city);
		}

		$user_addr = new Zend_Form_Element_Text('addrUserForm'); // address
		$user_addr->setLabel($translate->_("Address") )
		//->setRequired(true)
		->addFilter('StripTags')
		->addFilter('StringTrim')
		->setAttrib('size', 40)
		->setDecorators(array('Composite'))
		->addValidator($empty_validator);

		$phone_validator->setMin($others_config->min_homephone_length);
		$phone_validator->setMax($others_config->max_homephone_length);
		$user_tele = new Zend_Form_Element_Text('teleUserForm'); // telephone
		$user_tele->setLabel($translate->_("Telephone") )
		->addFilter('StripTags')
		->addFilter('StringTrim')
		->setAttrib('size', 15)
		//->setRequired(true)
		->setDecorators(array('Composite'))
		->addValidator($number_validator)
		->addValidator($phone_validator);

		$phone_validator->setMin($others_config->min_cellphone_length);
		$phone_validator->setMax($others_config->max_cellphone_length);
		$user_cell = new Zend_Form_Element_Text('cellUserForm'); // cellphone
		$user_cell->setLabel($translate->_("Cellphone") )
		->setRequired(true)
		->addFilter('StripTags')
		->addFilter('StringTrim')
		->setAttrib('size', 15)
		->setDecorators(array('Composite'))
		->addValidator($number_validator)
		->addValidator($phone_validator);

		$user_uname = new Zend_Form_Element_Text('unameUserForm'); // user name
		$user_uname->setLabel($translate->_("Username") )
		->setRequired(true)
		->addFilter('StripTags')
		->addFilter('StringTrim')
		->setAttrib('size', 15)
		->setDecorators(array('Composite'))
		->addValidator($empty_validator)
		->addValidator($uname_validator);

		$user_pass = new Zend_Form_Element_Password('passUserForm'); // user name
		$user_pass->setLabel($translate->_("Password") )
		->setRequired(true)
		->addFilter('StripTags')
		->addFilter('StringTrim')
		->setAttrib('size', 15)
		->setDecorators(array('Composite'))
		;

		$user_confirm_pass = new Zend_Form_Element_Password('cpassUserForm'); // user name
		$user_confirm_pass->setLabel($translate->_("Confirm password") )
		->setRequired(true)
		->addFilter('StripTags')
		->addFilter('StringTrim')
		->setAttrib('size', 15)
		->setDecorators(array('Composite'))
		;

		$user_supervisor = new Zend_Form_Element_Select('supeUserForm');
		$user_supervisor->setLabel($translate->_("Supervisor") )
						->setDecorators(array('Composite'));

		$users = new Users();
		$select = $users->select()->from($users, array('id', 'name'))
       			->where('type_id in (select id from vmg_user_type where access_level <' . $userType . ' and access_level >= ' . ($userType -1) . ' and access_level <= ' . $access_levels->salesman_level . ')');
		$rows = $users->fetchAll($select);
		if(count($rows) >= 1){
			foreach($rows as $row){
				$user_supervisor->addMultiOption($row->id, $row->name);
			};
		} else {
			foreach($types as $type){
				$user_supervisor->addMultiOption(0, $type->type_name);
			}
		}
		$submit = new Zend_Form_Element_Submit('submitUserForm');
		//$submit->setValue($translate->_('Add'));
		$submit->setDecorators(array('Composite'));

		$back = new Zend_Form_Element_Button('backUserForm');
		$back->setValue($translate->_('Back'));
		$back->setLabel('');
		$back->setAttrib('onclick', 'javascript: history.go(-1);');
		$back->setDecorators(array('Composite'));

		$this->addElements(array($id, $user_name, $user_type, $user_email, $user_city,
			$user_addr, $user_tele, $user_cell, $user_uname, $user_pass,
			$user_confirm_pass, $user_supervisor, $submit, $back));
	}
}
?>